import subprocess
import time

serverProc = None

if __name__ == "__main__":
    try:
        if time.localtime(time.time()).tm_hour >= 21:
            # 开启爬虫服务器
            serverProc = subprocess.Popen("python3 ~/Workspace/boxandneedle-scrapy/scrapy.py True", shell = True)

            time.sleep(2)

            # 开启定时任务
            while True:
                current_time = time.localtime(time.time())

                if ((current_time.tm_hour == 0) and (current_time.tm_min == 0) and (current_time.tm_sec == 0)):
                    subprocess.Popen("open -a \"/Applications/Google Chrome.app\" 'https://boxandneedle.taobao.com/search.htm'", shell = True)
                else:
                    print("{0}:{1}:{2}".format(current_time.tm_hour, current_time.tm_min, current_time.tm_sec))

                # 刻意让系统停顿一秒，否则在一秒钟之内while True会执行多次
                time.sleep(1)

    except KeyboardInterrupt:
        serverProc.kill()
